home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / leda / incl / range_tr.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  5.1 KB  |  195 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  range_tree.h
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. //------------------------------------------------------------------
  17. //
  18. // d-dimensional Range Trees
  19. //
  20. // Michael Wenzel     (1990)
  21. //
  22. // Implementation  follows
  23. // Kurt Mehlhorn: Data Structures and Algorithms, Vol.3
  24. //
  25. //------------------------------------------------------------------
  26.  
  27.  
  28. #ifndef RANGETREEH
  29. #define RANGETREEH
  30.  
  31.  
  32. #include <LEDA/list.h>
  33. #include <LEDA/bb_tree.h>
  34.  
  35.  
  36. //------------------------------------------------------------------
  37. // class tuple for range trees
  38. //
  39. // Michael Wenzel          (1990)
  40. //------------------------------------------------------------------
  41.  
  42.  
  43. //------------------------------------------------------------------
  44. // class tuple
  45. //------------------------------------------------------------------
  46.  
  47. class tuple {
  48.  
  49. friend class range_tree;
  50. friend class rm_tree;
  51.  
  52.   int d;                              // dimension of tuple
  53.   int vergl_dim;                      // comparision dimension
  54.   ent* t;
  55.   ent   inf;
  56.  
  57.  
  58.  void init(int );
  59.  
  60.  
  61.  public:
  62.  
  63.  ent& operator[](int i)  { return t[i];   }
  64.  ent& info()             { return inf; }
  65.  int  dimension()        { return d; }
  66.  
  67.  int  cmp_dim()      { return vergl_dim;    }
  68.  void set_cmp(int i) { if ((1<=i) && (i<=d)) vergl_dim=i;     }
  69.  
  70.  tuple& operator=(tuple& );
  71.  
  72.  tuple();
  73.  tuple(ent,ent);
  74.  tuple(ent,ent,ent);
  75.  tuple(int );
  76.  tuple(tuple& );
  77.  ~tuple();
  78.  
  79. OPERATOR_NEW(4)
  80. OPERATOR_DEL(4)
  81.  
  82. friend ent Copy(tuple& x)   { tuple* y=new tuple(x); return Ent(y); }
  83. friend void Clear(tuple& x) { for (int i=0; i<x.d; i++)
  84.                 Clear(x.t[i]);                      }
  85.  
  86.  
  87. };
  88.  
  89. typedef tuple* tuplep;
  90. typedef tuple* range_tree_item;
  91. typedef int (*TCMP)(tuplep,tuplep);
  92.  
  93.  
  94. // ------------------------------------------------------------------
  95. //  RANGE TREE
  96. // ----------------------------------------------------------------- 
  97.  
  98. class range_tree;
  99.  
  100. typedef range_tree* range_p;
  101.  
  102. //------------------------------------------------------------------------------
  103. // rm_tree = dictionary(tuplep,range_p)  (data structure: bb-alpha tree)
  104. //------------------------------------------------------------------------------
  105.  
  106.  
  107. declare(list,range_tree_item);
  108.  
  109.  
  110. struct rm_tree : public bb_tree {
  111.  
  112.  
  113.  
  114. int              defined(tuplep y)   { return bb_tree::member(Ent(y)); }
  115. bb_tree_item     lookup(tuplep y)    { return bb_tree::lookup(Ent(y)); }
  116. bb_tree_item     ord(int y)          { return bb_tree::ord(int(y)); }
  117. void             change_inf(bb_tree_item it, range_p i) 
  118.                                      { info(it) = i; }
  119. bb_tree_item     insert(tuplep y,range_p x)
  120.                                        { return bb_tree::insert(Ent(y),Ent(x)); } 
  121. void      del(tuplep y)                { delete bb_tree::del(Ent(y)); } 
  122. void      del_item(range_tree_item it) { del(it); } 
  123. range_p&  info(bb_tree_item it)        { return (range_p&)(bb_tree::info(it)); } 
  124. range_p&  inf(bb_tree_item it)         { return (range_p&)(bb_tree::info(it)); } 
  125.  
  126. } ;
  127.  
  128.  
  129. //------------------------------------------------------------------
  130. // class range_tree
  131. //------------------------------------------------------------------
  132.  
  133. class range_tree : public rm_tree {
  134.  
  135.   friend class bb_tree;
  136.   friend class bb_node;
  137.  
  138.   int     dim;                  // dimension 
  139.  
  140.   void lrot(bb_item , bb_item);
  141.   void rrot(bb_item , bb_item);
  142.   void ldrot(bb_item , bb_item);
  143.   void rdrot(bb_item , bb_item);
  144.   bool check(tuplep , tuplep , tuplep);
  145.  
  146. virtual void copy_key(ent& x)  const { x=x; }
  147. virtual void copy_inf(ent& x)  const { x=x; }
  148. virtual void clear_key(ent& x) const { x=0; }
  149. virtual void clear_inf(ent& x) const { x=0; }
  150.  
  151. virtual int  cmp_tuple_entry(int i, tuplep x, tuplep y) 
  152. { cout << "error: virtual cmp_tuple_entry\n"; i=0; x=y=0; return 0; }
  153.  
  154. virtual range_tree* new_range_tree(int i) 
  155. { cout << "error: virtual new_range_tree\n"; i=0; return 0; }
  156.  
  157. int tuple_cmp(tuplep x, tuplep y);  
  158.  
  159. int cmp(ent& x, ent& y) { return tuple_cmp(*(tuplep*)&x,*(tuplep*)&y); }
  160.  
  161. public :
  162.  
  163.   range_tree_item insert_tuple(tuplep);
  164.   void       del(tuplep);
  165.   tuplep     del_tuple(tuplep);
  166.   void       del_item(range_tree_item it)  { del_tuple(it); } 
  167.   void       query_tuples(tuplep,tuplep,dlist&);
  168.  
  169.   int dimension()                      { return dim;                 }
  170.   int dimension(bb_tree_item x)        { return key(x)->dimension(); }
  171.  
  172.   list(range_tree_item) all_tuples();
  173.  
  174.   tuplep            key(bb_tree_item p)  { return  tuplep(p->key()); }
  175.  
  176.   range_tree_item   min(int i);
  177.   range_tree_item   max(int i);
  178.                       
  179.   void              init_iterator()    { rm_tree::init_iterator(); }
  180.   bb_tree_item      move_iterator()    { return rm_tree::move_iterator(); }
  181.  
  182.   void              clear_tuple(tuplep& );
  183.   void              clear();
  184.   void              del_tree(bb_tree_item);
  185.  
  186.   range_tree(int i)               { dim = i; } 
  187.   ~range_tree();                 
  188.  
  189.  
  190. };
  191.  
  192. #endif
  193.  
  194.